<?php
// 获取文件名参数
$fileName = isset($_GET['file'])? $_GET['file'] : '';
$filePath = 'blog/'. $fileName;
if (!file_exists($filePath)) {
    die("文章文件不存在");
}
$fileContent = file_get_contents($filePath);

$parser = new DOMDocument();
@$parser->loadHTML($fileContent); // 使用 @ 抑制警告，因为 loadHTML 可能会产生一些关于 HTML 格式的警告

$title = $parser->getElementsByTagName('h1')->item(0)->textContent;
$author = $parser->getElementsByTagName('p')->item(0)->textContent;
$author = str_replace('作者: ', '', $author);
$content = $parser->getElementsByTagName('body')->item(0)->innerHTML;
$content = str_replace('<h1>'.$title.'</h1>', '', $content);
$content = str_replace('<p>作者: '.$author.'</p>', '', $content);
$category = $parser->getElementsByTagName('p')->item(2)->textContent;
$category = str_replace('分类: ', '', $category);
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑文章 - WeiLanBlog</title>
    <link href="https://fonts.googleapis.com/css2?family=SF+Pro+Display:wght@400;500;600&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'SF Pro Display', Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

       .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #0078d7;
        }

       .form-group {
            margin-bottom: 15px;
        }

       .form-group label {
            display: block;
            margin-bottom: 5px;
        }

       .form-group input,
       .form-group textarea,
       .form-group select {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #e0e0e0;
            border-radius: 5px;
        }

       .form-group button {
            background-color: #0078d7;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
        }

       .form-group button:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>编辑文章</h1>
        <form method="post" action="update.php">
            <input type="hidden" id="fileName" name="fileName" value="<?php echo htmlspecialchars($fileName);?>">
            <div class="form-group">
                <label for="title">文章标题</label>
                <input type="text" id="title" name="title" value="<?php echo htmlspecialchars($title);?>" required>
            </div>
            <div class="form-group">
                <label for="author">文章作者</label>
                <input type="text" id="author" name="author" value="<?php echo htmlspecialchars($author);?>" required>
            </div>
            <div class="form-group">
                <label for="content">文章内容</label>
                <textarea id="content" name="content" required><?php echo htmlspecialchars($content);?></textarea>
            </div>
            <div class="form-group">
                <label for="category">文章分类</label>
                <input type="text" id="category" name="category" value="<?php echo htmlspecialchars($category);?>" required>
            </div>
            <button type="submit">保存修改</button>
        </form>
    </div>
</body>

</html>
